package com.distribution.system.openapi.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.distribution.system.domain.bo.UnOrderResultBo;
import com.distribution.system.domain.req.ReqResult;
import com.distribution.system.openapi.domain.ApiResult;
import com.distribution.system.openapi.domain.dto.ApiChoosePhoneDTO;
import com.distribution.system.openapi.domain.dto.ApiOrderSearchDTO;
import com.distribution.system.openapi.domain.dto.ApiSendCodeDTO;
import com.distribution.system.openapi.domain.dto.ApiPlaceOrderDTO;
import com.distribution.system.openapi.service.CardDistributionApiService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author: GodLu
 * @create: 2024-04-15 10:17
 * @description: 号卡分发三方接口
 */
@Slf4j
@RestController
@RequestMapping("/api/sparkPlan1")
public class ProductDistributionApiController {
    private static final Logger partnerChoosePhoneLog = LoggerFactory.getLogger("partnerChoosePhoneLog");
    private static final Logger partnerPlaceOrderLog = LoggerFactory.getLogger("partnerPlaceOrderLog");
    private static final Logger partnerSearchOrderLog = LoggerFactory.getLogger("partnerSearchOrderLog");
    private static final Logger partnerSmsCodeLog = LoggerFactory.getLogger("partnerSmsCodeLog");
    @Resource
    CardDistributionApiService companyProductOperationService;

    @PostMapping("/sendCode")
    public ReqResult sendVerifyCode(@RequestBody ApiSendCodeDTO apiSendCodeDTO){
        log.info("下游接口获取验证码入参:{}",JSONObject.toJSONString(apiSendCodeDTO));
        partnerSmsCodeLog.info("下游接口获取验证码入参:{}",JSONObject.toJSONString(apiSendCodeDTO));
        ReqResult reqResult = companyProductOperationService.sendVerifiedCode(apiSendCodeDTO);
        log.info("下游接口获取验证码返回:{}",reqResult);
        partnerSmsCodeLog.info("下游接口获取验证码返回:{}",reqResult);
        return reqResult;
    }

    @PostMapping("/choosePhone")
    public ReqResult choosePhoneNumber(@RequestBody ApiChoosePhoneDTO apichoosePhoneDTO) {
        log.info("下游接口选号入参:{}",JSONObject.toJSONString(apichoosePhoneDTO));
        partnerChoosePhoneLog.info("下游接口选号入参:{}",JSONObject.toJSONString(apichoosePhoneDTO));
        ReqResult reqResult = companyProductOperationService.choosePhoneNumber(apichoosePhoneDTO);
        log.info("下游接口选号返回:{}",reqResult);
        partnerChoosePhoneLog.info("下游接口选号返回:{}",reqResult);
        return reqResult;
    }

    @RequestMapping("/unOrder")
    public ReqResult unOrder(@RequestBody ApiPlaceOrderDTO apiPlaceOrderDTO){
        log.info("下游接口下单入参:{}",JSONObject.toJSONString(apiPlaceOrderDTO));
        partnerPlaceOrderLog.info("下游接口下单入参:{}",JSONObject.toJSONString(apiPlaceOrderDTO));
        ReqResult reqResult = companyProductOperationService.placeOrder(apiPlaceOrderDTO);
        if (reqResult.getCode() == 200) {
            UnOrderResultBo unOrderResultBo = (UnOrderResultBo) reqResult.getData();
            reqResult.setData(unOrderResultBo);
        }
        log.info("下游接口下单返回:{}",reqResult);
        partnerPlaceOrderLog.info("下游接口下单返回:{}",reqResult);
        return reqResult;
    }

    @PostMapping("/orderSearch")
    public ApiResult orderSearch(@RequestBody @Validated ApiOrderSearchDTO apiOrderSearchDTO) {
        log.info("下游接口查单入参:{}",JSONObject.toJSONString(apiOrderSearchDTO));
        partnerSearchOrderLog.info("下游接口查单入参:{}",JSONObject.toJSONString(apiOrderSearchDTO));
        JSONArray jsonArray = companyProductOperationService.searchOrder(apiOrderSearchDTO);
        ApiResult result = ApiResult.success(jsonArray);
        log.info("下游接口查单返回:{}",result.getData());
        partnerSearchOrderLog.info("下游接口查单返回:{}",result.getData());
        return result;
    }
}
